package cgd;

import cdp.Cliente;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.persistence.Query;
import javax.persistence.TemporalType;

public class ClienteDao extends DaoJpa<Cliente> {

    private static ClienteDao instance = null;

    public static ClienteDao getInstance() {
        if (instance == null) {
            instance = new ClienteDao();
        }
        return instance;
    }

    public List<Cliente> obterTodos() throws Exception {
        return buscar(new Cliente());
    }

    public Cliente consultar(int id) throws Exception {
        try {
            Query q = em.createQuery("SELECT t FROM Cliente t WHERE t.id = " + id);
            return (Cliente) q.getResultList().get(0);
        } catch (Exception ex) {
            throw ex;
        }
    }

    public List<Cliente> consultarPeriodo(Date date) throws Exception {
        try {
            Query q = em.createQuery("SELECT t FROM Cliente t WHERE t.dataCadastro BETWEEN ?1  AND CURRENT_DATE ");
            q.setParameter(1, date, TemporalType.DATE);            
            return q.getResultList();
        } catch (Exception ex) {
            throw ex;
        }
    }

    public Cliente verificarLogin(String nome, String senha) throws Exception {
        try {
            Query q = em.createQuery("SELECT c FROM Cliente c");
            for (Cliente cliente : (List<Cliente>) q.getResultList()) {
                if (cliente.getNome().equals(nome) && cliente.getSenha().equals(senha)) {
                    return cliente;
                }
            }
        } catch (Exception ex) {
            throw ex;
        }
        return null;
    }
}
